package me.ivenapp.gomoku;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.AbilityInfo;
import ohos.global.configuration.Configuration;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.system.DeviceInfo;

public abstract class AbilityBase extends Ability {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x1001, AbilityBase.class.getSimpleName());
    private IConfigurationUpdatedListener configurationUpdatedListener;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //设置沉浸式状态栏，页面会扩展至状态栏
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);

        Configuration configuration = getResourceManager().getConfiguration();
        if(configuration.direction != Configuration.DIRECTION_VERTICAL) {
            if("phone".equals(DeviceInfo.getDeviceType()) || "default".equals(DeviceInfo.getDeviceType())) {
//            设置当前Ability为竖屏
                setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
            }
        }
    }

    /**
     * 供Slice设置监听配置变化
     * @param listener
     */
    public void setConfigurationUpdatedListener(IConfigurationUpdatedListener listener) {
        configurationUpdatedListener = listener;
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
//        HiLog.debug(TAG, "onConfigurationUpdated");
        Configuration old = getResourceManager().getConfiguration();
        if(old.direction != Configuration.DIRECTION_VERTICAL) {
            if("phone".equals(DeviceInfo.getDeviceType()) || "default".equals(DeviceInfo.getDeviceType())) {
//            设置当前Ability为竖屏
                setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
            }
        }
        if(configurationUpdatedListener != null) {
            configurationUpdatedListener.onUpdated(configuration);
        }
    }

    public interface IConfigurationUpdatedListener {
        void onUpdated(Configuration newConfig);
    }
}
